package inet.app.view.activity;

import inet.app.R;
import inet.app.common.OnLoginListener;
import inet.app.common.XoSoConstants;
import inet.app.controller.connect.JsonParser;
import inet.app.controller.connect.MasterConnector;
import inet.app.controller.database.DBConfig;
import inet.app.controller.database.DatabaseManager;
import inet.app.controller.request.Request;
import inet.app.controller.request.Request_Format;
import inet.app.model.object.KqGiaiRegion;
import inet.app.model.object.Member;
import inet.app.model.util.DateProc;
import inet.app.model.util.MyPreferenceManager;
import inet.app.model.util.ProgressDialog;
import inet.app.model.util.SmartLog;
import inet.app.model.util.Util;
import inet.app.view.dialog.InfoMemberDialog;
import inet.app.view.dialog.LoginDialog;
import inet.app.view.slidemenu.SlideMenu;
import inet.app.view.slidemenu.SlideMenuAnimationContainer;
import inet.app.view.slidemenu.SlideMenuAnimationContainer.MenuItemSelectedAction;
import inet.app.view.slidemenu.SlideMenuItem;
import inet.app.view.slidemenu.SlideMenuItem.SlideMenuItemType;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class BaseFragmentActivity extends FragmentActivity {

	private MasterConnector master;
	int heavyblue = Color.rgb(0, 0, 255);
	protected Request request = new Request();
	protected Button btKQXSAbout, btKQXSSearch;
	protected String date_current;
	protected TextView tvtieude;
	protected Dialog help, about, dlquit;
	protected int demtrang = 1;
	protected int max = 10;
	public DisplayMetrics metrics;
	protected Thread myCommsThread = null;
	protected ImageView imgThongtin;
	private TextView tvNhamang;
	protected ProgressDialog pdia;
	/** Called when the activity is first created. */
	// SLIDE MENU
	public SlideMenuAnimationContainer slideMenuAnimationContainer;
	public List<SlideMenuItem> slideMenuList = new ArrayList<SlideMenuItem>();
	public SlideMenu slideMenu;
	protected MyApp mApp;
	protected DatabaseManager mDbManager;

	// KetQuaFragment
	protected Thread pushThread = null;
	protected boolean isRunning = true;

	protected boolean isFree;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// hide the keyboard everytime the activty starts
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		pdia = new ProgressDialog(BaseFragmentActivity.this);
		date_current = DateProc.Timestamp2DDMMYYYY(DateProc.createTimestamp());
		mApp = (MyApp) getApplication();
		mDbManager = DatabaseManager.instance(getApplicationContext());
		isFree = MyPreferenceManager.getInstance(getApplicationContext())
				.isFree();
	}

	@Override
	public void onResume() {
		if (((MyApp) getApplication()).isKill())
			finish();
		super.onResume();
	}

	protected void initBase() {
		initMenu();
	}

	public void initMenu() {

		updateMenuLogin();
		slideMenuList.clear();
		tvNhamang = (TextView) findViewById(R.id.tvNhamang);
		tvNhamang.setText(Util.getSharePreferences(BaseFragmentActivity.this));
		slideMenuAnimationContainer = (SlideMenuAnimationContainer) findViewById(R.id.slideMenuAnimationContainer);
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_0));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_1));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_2));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_3));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_4));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_5));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_6));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_7));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_8));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_9));
		Member member = MyPreferenceManager
				.getInstance(getApplicationContext()).getMember();
		if (member != null)
			slideMenuList
					.add(new SlideMenuItem(SlideMenuItemType.Activity,
							R.drawable.menu_icon, Util.MENU_10X
									+ member.getUsername()));
		else
			slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
					R.drawable.menu_icon, Util.MENU_10));

		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				R.drawable.menu_icon, Util.MENU_11));

		slideMenu = new SlideMenu(this, slideMenuAnimationContainer);
		slideMenuAnimationContainer.setListener(slideMenu);

		slideMenu.init(slideMenuList);
		System.out.println("KHONOG NEN CHAY VAO FAY " + MyApp.lastPosition);
		slideMenuAnimationContainer
				.setMenuItemSelectedAction(new MenuItemSelectedAction() {
					@Override
					public void execute(AdapterView<?> parent, View view,
							int position, long id) {
						final Bundle rb = new Bundle();
						switch (position) {
						case 0:
							// ket qua xo so
							if (position != MyApp.lastPosition) {

								hideMenu();
								pdia.show();
								final Handler handler = new Handler() {
									@Override
									public void handleMessage(Message msg) {
										super.handleMessage(msg);
										if (msg.what == 0) {
											String json = (String) msg.obj;
											Intent intent = new Intent(
													BaseFragmentActivity.this,
													KetQuaFragment.class);
											Bundle rb = new Bundle();
											rb.putString("json", json);
											intent.putExtras(rb);
											MyApp.lastPosition = XoSoConstants.posKetQua;
											startActivity(intent);
											pdia.dismiss();
											finish();
										} else if (msg.what == 1) {
											pdia.dismiss();
											Util.showDialog(1,
													BaseFragmentActivity.this);
										}
									}
								};
								Thread thread = new Thread() {
									public void run() {
										String date_current = DateProc
												.Timestamp2DDMMYYYY(DateProc
														.createTimestamp());
										if (Util.onBeforeLiveTime()) {
											date_current = DateProc.TimestampPlusDay2DDMMYYYY(
													DateProc.createTimestamp(),
													-1);
										}
										// find result in db
										KqGiaiRegion regionMB = DatabaseManager
												.instance(
														BaseFragmentActivity.this)
												.getDaoKqXSRegion()
												.selectRow(date_current, "MB");

										if (regionMB != null) {

											// load kqxs mb from db
											SmartLog.d("load kqxs mb "
													+ date_current
													+ " from database success",
													date_current);
											Message msg = handler
													.obtainMessage(
															0,
															XoSoConstants.GET_KQXS_FROM_DB);
											handler.sendMessage(msg);
										} else {
											JsonParser jParser = new JsonParser();
											String jobject = jParser.getURL(request
													.getkqxsbyregion(
															date_current, "MB"));
											String dttt = jParser.getURL(request
													.getxsdttt(date_current));
											String kaka = jobject + "_____"
													+ dttt;
											if (jobject != null) {
												Message msg = handler
														.obtainMessage(0, kaka);
												handler.sendMessage(msg);
											} else {
												Message msg = handler
														.obtainMessage(1);
												handler.sendMessage(msg);
											}
										}
									}
								};
								thread.start();
							}
							break;
						case 1:

							// tu van
							if (position != MyApp.lastPosition) {

								if (BaseFragmentActivity.this instanceof KetQuaFragment) {
									stopThread();
								}
								hideMenu();
								pdia.show();
								if (!mApp.isFirstLaunchTuVan
										&& mDbManager.getDaoTinhSc()
												.isExistData(
														DBConfig.TABLE_TINH_SC)) {
									// exist db
									SmartLog.d("get tinh sc from db");
									Intent intent_tv = new Intent(
											BaseFragmentActivity.this,
											VipFragment.class);
									Bundle bun = new Bundle();
									bun.putString("json",
											XoSoConstants.GET_TINH_SC_FROM_DB);
									intent_tv.putExtras(bun);
									MyApp.lastPosition = XoSoConstants.posTuVan;
									startActivity(intent_tv);
									pdia.dismiss();
									finish();
								} else {
									// get from server
									SmartLog.d("get tinh sc from server");
									final Handler handler_tv2 = new Handler() {
										@Override
										public void handleMessage(Message msg) {
											super.handleMessage(msg);
											if (msg.what == 0) {
												String json = (String) msg.obj;
												System.out
														.println("JSON GỬI NDI === "
																+ json);
												Intent intent_tv = new Intent(
														BaseFragmentActivity.this,
														VipFragment.class);
												Bundle rb = new Bundle();
												rb.putString("json", json);
												intent_tv.putExtras(rb);
												MyApp.lastPosition = XoSoConstants.posTuVan;
												startActivity(intent_tv);
												pdia.dismiss();
												finish();
											} else if (msg.what == 1) {
												pdia.dismiss();
												Util.showDialog(
														1,
														BaseFragmentActivity.this);
											}
										}
									};
									Thread thread_tv2 = new Thread() {
										public void run() {
											JsonParser jParser = new JsonParser();
											System.out
													.println("DATE  CURREN T "
															+ date_current);
											String jobject = jParser.getURL(request
													.getlisttinhsc(date_current));
											if (jobject != null) {
												mApp.isFirstLaunchTuVan = false;
												Message msg = handler_tv2
														.obtainMessage(0,
																jobject);
												handler_tv2.sendMessage(msg);
											} else {
												SmartLog.e("get tinh sc from server failed");
												// check db
												if (mDbManager
														.getDaoTinhSc()
														.isExistData(
																DBConfig.TABLE_TINH_SC)) {
													SmartLog.d("get tinh sc from db");
													Message msg = handler_tv2
															.obtainMessage(
																	0,
																	XoSoConstants.GET_TINH_SC_FROM_DB);
													handler_tv2
															.sendMessage(msg);
												} else {
													// error
													Message msg = handler_tv2
															.obtainMessage(1);
													handler_tv2
															.sendMessage(msg);
												}
											}

										}
									};
									thread_tv2.start();
								}
							}
							break;
						case 2:

							// so mo
							if (position != MyApp.lastPosition) {

								if (BaseFragmentActivity.this instanceof KetQuaFragment) {
									stopThread();
								}
								hideMenu();
								Intent intent = new Intent(
										BaseFragmentActivity.this,
										SomoActivity.class);
								MyApp.lastPosition = XoSoConstants.posSoMo;
								startActivity(intent);
								finish();
							}
							break;
						case 3:
							// thong ke
							if (position != MyApp.lastPosition) {

								if (BaseFragmentActivity.this instanceof KetQuaFragment) {
									stopThread();
								}
								hideMenu();
								pdia.show();
								final Handler handler_tk = new Handler() {
									@Override
									public void handleMessage(Message msg) {
										super.handleMessage(msg);
										if (msg.what == 0) {
											String json = (String) msg.obj;
											Intent intentTk = new Intent(
													BaseFragmentActivity.this,
													ThongKeFragment.class);
											Bundle rb = new Bundle();
											rb.putString("json", json);
											intentTk.putExtras(rb);
											MyApp.lastPosition = XoSoConstants.posThongKe;
											startActivity(intentTk);
											pdia.dismiss();
											finish();
										} else if (msg.what == 1) {
											pdia.dismiss();
											Util.showDialog(1,
													BaseFragmentActivity.this);
										}
									}

								};
								Thread thread_tk = new Thread() {
									public void run() {

										JsonParser jParser = new JsonParser();
										String jobject = jParser.getURL(request
												.gettk("XSTD", "10", "null"));
										if (jobject != null) {
											Message msg = handler_tk
													.obtainMessage(0, jobject);
											handler_tk.sendMessage(msg);
										} else {

											Message msg = handler_tk
													.obtainMessage(1);
											handler_tk.sendMessage(msg);
										}

									}
								};
								thread_tk.start();
							}
							break;
						case 4:

							// mo thuong
							if (position != MyApp.lastPosition) {

								if (BaseFragmentActivity.this instanceof KetQuaFragment) {
									stopThread();
								}
								hideMenu();
								pdia.show();
								Intent intentLmt_tn = new Intent(
										BaseFragmentActivity.this,
										LichMoThuongFragment.class);
								MyApp.lastPosition = XoSoConstants.posMoThuong;
								startActivity(intentLmt_tn);
								pdia.dismiss();
								finish();
							}
							break;
						case 5:
							// do so
							if (position != MyApp.lastPosition) {

								if (BaseFragmentActivity.this instanceof KetQuaFragment) {
									stopThread();
								}
								hideMenu();
								pdia.show();
								Intent intent_ds = new Intent(
										BaseFragmentActivity.this,
										DoSoActivity.class);
								MyApp.lastPosition = XoSoConstants.posDoSo;
								startActivity(intent_ds);
								pdia.dismiss();
								finish();
							}
							break;
						case 6:

							// chon ve so
							if (position != MyApp.lastPosition) {

								if (BaseFragmentActivity.this instanceof KetQuaFragment) {
									stopThread();
								}
								hideMenu();
								pdia.show();
								Intent intentVeso = new Intent(
										BaseFragmentActivity.this,
										VesoList.class);
								rb.putString("datasend", "");
								intentVeso.putExtras(rb);
								MyApp.lastPosition = XoSoConstants.posChonVeSo;
								startActivity(intentVeso);
								pdia.dismiss();
								finish();
							}
							break;
						case 7:
							// quay thu
							if (position != MyApp.lastPosition) {

								if (BaseFragmentActivity.this instanceof KetQuaFragment) {
									stopThread();
								}
								hideMenu();
								pdia.show();
								Intent intentQt = new Intent(
										BaseFragmentActivity.this,
										QuaythuActivity.class);
								MyApp.lastPosition = XoSoConstants.posQuayThu;
								startActivity(intentQt);
								pdia.dismiss();
								finish();
							}
							break;
						case 8:
							// moi ban be
							if (position != MyApp.lastPosition) {
								MyApp.lastPosition = XoSoConstants.posInviteFriends;
								if (BaseFragmentActivity.this instanceof KetQuaFragment) {
									stopThread();
								}
								hideMenu();
								Intent intent = new Intent(
										BaseFragmentActivity.this,
										InviteFriendsActivity.class);
								startActivity(intent);
								finish();
							}
							break;
						case 9:
							// huong dan
							if (position != MyApp.lastPosition) {
								// MyApp.lastPosition =
								// DeveloperConfig.posHuongDan;
								hideMenu();
								helpDialog(BaseFragmentActivity.this);
								// help.show();
							}
							break;
						case 10:
							// Đăng Nhập,đăng ký
							if (position != MyApp.lastPosition) {
								// MyApp.lastPosition =
								// DeveloperConfig.posThongTin;
								removePreviousDialog("login_dialog");
								if (MyPreferenceManager.getInstance(
										getApplicationContext()).getMember() == null) {
									LoginDialog
											.newInstance(
													BaseFragmentActivity.this)
											.setLoginListener(mLoginListener)
											.show(BaseFragmentActivity.this
													.getSupportFragmentManager(),
													"login_dialog");
								} else {
									InfoMemberDialog
											.newInstance(
													BaseFragmentActivity.this)
											.setMember(
													MyPreferenceManager
															.getInstance(
																	getApplicationContext())
															.getMember())
											.setLogoutListener(mLoginListener)
											.show(BaseFragmentActivity.this
													.getSupportFragmentManager(),
													"info_dialog");
								}
							}
							break;
						case 11:
							// thoat
							if (position != MyApp.lastPosition) {
								// MyApp.lastPosition =
								// DeveloperConfig.posThoat;
								showQuitDialog(BaseFragmentActivity.this);
							}
							break;
						default:
							break;
						}

					}
				});

	}

	protected void hideMenu() {
		if (slideMenuAnimationContainer.isOpening()) {
			slideMenuAnimationContainer.closeSlideMenu();
		}
	}

	public void showQuitDialog(Context c) {
		dlquit = new Dialog(c);
		dlquit.requestWindowFeature(Window.FEATURE_LEFT_ICON);
		dlquit.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		dlquit.setTitle("Thoát ứng dụng ?");
		dlquit.setContentView(R.layout.dialog_quit);
		
		dlquit.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.rgb(50, 50, 50)));
		dlquit.getWindow().setTitleColor(XoSoConstants.White);
		dlquit.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.icon_registersc);

		Button huy = (Button) dlquit.findViewById(R.id.bthuy);
		huy.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				dlquit.dismiss();
			}
		});

		Button quit = (Button) dlquit.findViewById(R.id.btquit);
		quit.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				((MyApp) getApplication()).setKill(true);
				Util.savePreferences("quit", true, BaseFragmentActivity.this);
				if (!Util.onLiveTime_MB()) {
					Util.savePreferences("ONLIVEMB", false,
							BaseFragmentActivity.this);
				}
				if (!Util.onLiveTime_MT()) {
					Util.savePreferences("ONLIVEMT", false,
							BaseFragmentActivity.this);
				}
				if (!Util.onLiveTime_MN()) {
					Util.savePreferences("ONLIVEMN", false,
							BaseFragmentActivity.this);
				}
				quitThread.start();
				finish();
				dlquit.dismiss();
			}
		});
		dlquit.show();
	}

	Thread quitThread = new Thread(new Runnable() {

		@Override
		public void run() {
			master = new MasterConnector(
					((MyApp) getApplication()).get_socket());
			master.getRequest(Request_Format.Discon_Req());
		}
	});

	public void helpDialog(Context c) {
		help = new Dialog(c);
		
		help.setTitle("Trợ giúp");
		help.setContentView(R.layout.dialog_help);
		help.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

 		final ImageView img = (ImageView) help.findViewById(R.id.imgTroGiup);
		Button trogiupdong = (Button) help.findViewById(R.id.btTroGiupDong);
		Button trogiuptruoc = (Button) help.findViewById(R.id.btTroGiupTruoc);
		Button trogiupsau = (Button) help.findViewById(R.id.btTroGiupSau);
		final TextView tvnoidung = (TextView) help.findViewById(R.id.tvTroGiup);
		final TextView tvnoidung2 = (TextView) help
				.findViewById(R.id.tvTroGiup2);
		final TextView tvsotrang = (TextView) help.findViewById(R.id.tvSoTrang);
		tvsotrang.setText(demtrang + "/" + max);
		tvnoidung.setText(R.string.trogiup1);
		tvnoidung2.setText(R.string.trogiup8);
		img.setImageResource(R.drawable.icon_3mien);

		trogiupdong.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				help.dismiss();
			}
		});

		trogiuptruoc.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (demtrang > 1)
					demtrang--;
				tvsotrang.setText(demtrang + "/" + max);
				switch (demtrang) {
				case 1:
					tvnoidung.setText(R.string.trogiup1);
					tvnoidung2.setText(R.string.trogiup8);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 2:
					tvnoidung.setText(R.string.trogiup2);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 3:
					tvnoidung.setText(R.string.trogiup3);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 4:
					tvnoidung.setText(R.string.trogiup4);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 5:
					tvnoidung.setText(R.string.trogiup5);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 6:
					tvnoidung.setText(R.string.trogiup6);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 7:
					tvnoidung.setText(R.string.trogiup7);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 8:
					tvnoidung.setText(R.string.trogiup9);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 9:
					tvnoidung.setText(R.string.trogiup10);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 10:
					tvnoidung.setText(R.string.trogiup11);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				}
			}
		});
		trogiupsau.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (demtrang < max)
					demtrang++;
				tvsotrang.setText(demtrang + "/" + max);
				switch (demtrang) {
				case 1:
					tvnoidung.setText(R.string.trogiup1);
					tvnoidung2.setText(R.string.trogiup8);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 2:
					tvnoidung.setText(R.string.trogiup2);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 3:
					tvnoidung.setText(R.string.trogiup3);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 4:
					tvnoidung.setText(R.string.trogiup4);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 5:
					tvnoidung.setText(R.string.trogiup5);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 6:
					tvnoidung.setText(R.string.trogiup6);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 7:
					tvnoidung.setText(R.string.trogiup7);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 8:
					tvnoidung.setText(R.string.trogiup9);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 9:
					tvnoidung.setText(R.string.trogiup10);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				case 10:
					tvnoidung.setText(R.string.trogiup11);
					img.setImageResource(R.drawable.icon_3mien);
					break;
				}
			}
		});
		help.show();
	}

	public void showAboutDialog(Context c) {
		about = new Dialog(c);
		about.setTitle("              Thông Tin");
		about.setContentView(R.layout.dialog_about);
		Button dong = (Button) about.findViewById(R.id.btAboutClose);
		dong.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				about.dismiss();
			}
		});
		about.show();
	}

	/**
	 * Remove previous show dialog fragment by tag
	 * 
	 * @param tag
	 *            tag of dialog fragment
	 */
	protected void removePreviousDialog(String tag) {
		try {
			// DialogFragment.show() will take care of adding the fragment
			// in a transaction. We also want to remove any currently showing
			// dialog, so make our own transaction and take care of that here.
			FragmentManager fm = getSupportFragmentManager();
			Fragment prev = fm.findFragmentByTag(tag);
			if (prev != null) {
				FragmentTransaction ft = fm.beginTransaction();
				ft.remove(prev);
				ft.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void stopThread() {
		isRunning = false;
		((MyApp) getApplication()).setRunning(false);
		if (pushThread != null) {
			pushThread.interrupt();
			pushThread = null;
		}
	}

	/******************************** Login *****************************/

	public boolean isLogin() {
		return (MyPreferenceManager.getInstance(getApplicationContext())
				.getMember() != null);
	}

	@SuppressWarnings("deprecation")
	private void updateMenuLogin() {
		imgThongtin = (ImageView) findViewById(R.id.imgThongtin);
		boolean isLogin = isLogin();
		imgThongtin.setBackgroundDrawable(isLogin ? getResources().getDrawable(
				R.drawable.button_thongtin_select) : getResources()
				.getDrawable(R.drawable.button_thongtin));
		imgThongtin.setOnClickListener(isLogin ? OnShowInfoDialogListener
				: OnShowLoginDialogListener);
	}

	private View.OnClickListener OnShowLoginDialogListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			removePreviousDialog("login_dialog");
			LoginDialog
					.newInstance(BaseFragmentActivity.this)
					.setLoginListener(mLoginListener)
					.show(BaseFragmentActivity.this.getSupportFragmentManager(),
							"login_dialog");
		}
	};

	private View.OnClickListener OnShowInfoDialogListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			removePreviousDialog("info_dialog");
			InfoMemberDialog
					.newInstance(BaseFragmentActivity.this)
					.setMember(
							MyPreferenceManager.getInstance(
									getApplicationContext()).getMember())
					.setLogoutListener(mLoginListener)
					.show(BaseFragmentActivity.this.getSupportFragmentManager(),
							"info_dialog");
		}
	};
	public OnLoginListener mLoginListener = new OnLoginListener() {

		@Override
		public void onStart() {
			pdia.show();
		}

		@Override
		public void onFinish() {
			pdia.dismiss();
			updateMenuLogin();
			// TODO update slide menu
			initMenu();
		}

		@Override
		public void onLogout() {
			MyPreferenceManager.getInstance(getApplicationContext())
					.clearMember();
			updateMenuLogin();
			initMenu();
		}
	};

	@Override
	protected void onDestroy() {
		// android.os.Process.killProcess(android.os.Process.myPid());
		super.onDestroy();
	}
}